package cap.util;

import cap.bean.Counter;
import cap.dao.CounterDao;
import cap.dao.impl.CounterDaoImpl;


import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

@WebListener
public class CountOnlineListener implements HttpSessionListener {//监听类
	private CounterDao cntDao=new CounterDaoImpl();


	public void sessionCreated(HttpSessionEvent hse)
	{
		Counter cnt = cntDao.getCounter();
		int num = cnt.getCount();
		HttpSession session = hse.getSession();
		if (session.isNew()) {
			num = num + 1;
		}
		session.setAttribute("num", num);
	}

	public void sessionDestroyed(HttpSessionEvent hse)
	{
		HttpSession session = hse.getSession();
		int num = (Integer)session.getAttribute("num");
		cntDao.setNum(num);
	}
}
